<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<div class="am-modal" tabindex="-1" id="startPrintmodal">
	<div class="am-modal-dialog">
		<div class="am-modal-hd">
			选择打印机<a href="javascript: void(0)" class="am-close"
				data-am-modal-close>&times;</a>
		</div>
		<div class="am-modal-bd">
			<form action="<%=request.getContextPath() %>/startPrint"
				target="_blank" method="post" class="am-form"
				id="startPrintForm">
				<div class="am-g am-margin-top">
					<div class="am-u-sm-4 am-u-md-2 am-text-right">打印机</div>
					<div class="am-u-sm-8 am-u-md-10">
						<select data-am-selected="{btnSize: 'sm',maxHeight:200}" name="printService">
							<option value="">请选择打印机</option>
							
						</select>
					</div>
				</div>
			</form>
		</div>
		<div class="am-modal-footer">
			<span class="am-modal-btn" data-am-modal-confirm>打&nbsp;&nbsp;印</span>
			<span class="am-modal-btn" data-am-modal-cancel>取&nbsp;&nbsp;消</span>
		</div>
	</div>
</div>

<script>
	function startPrint(printService){
		var $selected = $("#startPrintForm select[name='printService']");
		$selected.find("option").remove();
		$selected.append("<option value=''>请选择打印机</option>");
		for(var i = 0;i < printService.length;i++){
			$selected.append("<option value='"+printService[i]["name"]+"'>"+printService[i]["name"]+"</option>");
		}
		$selected.trigger('changed.selected.amui');
		
		$('#startPrintmodal').modal({
			relatedTarget: this,
			closeOnConfirm: false,
			onConfirm: function(e) {
				var $selectedPrintService = $("#startPrintForm select[name='printService']>option:checked");
				var mcLength = $selectedPrintService.length;
				if(mcLength > 0){
					for(var i = 0;i < printService.length;i++){
						if(printService[i]["name"] == $selectedPrintService.val()){
							if(!printService[i]["Sides"]){
								layer.confirm('当前打印机不支持双面打印，是否继续打印？', {
									btn: ['继续打印','放弃打印']
								}, function(){
									submitStartPrintForm();
								});
							}
							else{
								submitStartPrintForm();
							}
						}
					}
				}
				else{
					layer.msg("请选择打印机", {time: 1500});
				}
			}
		});
	}
	
	function submitStartPrintForm(){
		$("#startPrintForm").ajaxSubmit({
			success : function(result) {
				switch(result){
				case "success": 
					layer.msg("正在打印中，请稍后！", {time: 1500});
					$("#loading-modal").modal("close");
					break;
				case "not_exists": layer.msg("无法获取到您所选打印机的服务，请重新选择！", {time: 1500});
					break;
				case "error": layer.msg("打印失败，请尝试重新打印！", {time: 1500});
					break;
				}
			},
			error : function(){
				$("#loading-modal").modal("close");
				layer.msg("打印失败，请尝试重新打印！", {time: 1500});
			},
			dataType: "html",
			timeout: 30 * 1000  
		});
	}
</script>